package cn.zhizihua.service.impl;

import cn.zhizihua.mapper.CartItemMapper;
import cn.zhizihua.pojo.CartItem;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.query.CartItemQuery;
import cn.zhizihua.service.ICartItemService;
import cn.zhizihua.vo.PageBean;
import cn.zhizihua.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CartItemService implements ICartItemService {
    @Autowired  //自动注入
    CartItemMapper cartItemMapper;

    @Transactional  //spring事务处理
    @Override
    public ResultBean add(CartItem cartItem) {
        //参数校验
        if (cartItem == null ||
                cartItem.getUserId() == null || cartItem.getUserId() == 0 ||
                cartItem.getBusinessId() == null || cartItem.getBusinessId() == 0 ||
                cartItem.getFoodId() == null || cartItem.getFoodId() == 0 ||
                cartItem.getQuantity() == null || cartItem.getQuantity() == 0
        ){
            return ResultBean.ERROR(10001,"CartItemService-add 请求参数为空");
        }

        /*  ---------------------------判断是否已经有该购物车项-------------------------
        1、先使用foodId、businessId、userId进行搜索  （判断在此购物车项是否已经存在）
        2、如果存在  修改数量
        3、如果不存在  添加
         */
        //搜索是否已经有此购物车项
        CartItemQuery cartItemQuery = new CartItemQuery(cartItem.getFoodId(),cartItem.getBusinessId(),cartItem.getUserId()); //查询条件
        List<CartItem> cartItemList = cartItemMapper.getByCondition(cartItemQuery);
        if (cartItemList.size()>0){  //size大于0 已经有此购物车项了
            CartItem target = cartItemList.get(0);  //获取此购物车项  target含有完整的购物车项数据（cartId,foodId,businessId,userId,quantity）

            //修改数量  要进行update需要cartId，所以新数量必须赋值给target（非cartItem）
            target.setQuantity(target.getQuantity()+cartItem.getQuantity());
            int update = cartItemMapper.update(target);
            return update>0?ResultBean.SUCCESS():ResultBean.ERROR("CartItemService-add&update 添加购物车项失败");
        }

        //---------------如果执行到这一步 则说明没有该购物车项 正常进行添加操作---------------
        int add = cartItemMapper.add(cartItem);  //购物车项为空  进行添加
        return add>0?ResultBean.SUCCESS(cartItem.getCartId()):ResultBean.ERROR("CartItemService-add 添加购物车项失败");
    }

    @Transactional
    @Override
    public ResultBean update(CartItem cartItem) {
        //参数校验
        if (cartItem == null ||
                cartItem.getCartId() == null || cartItem.getCartId() == 0 ||
                cartItem.getUserId() == null || cartItem.getUserId() == 0 ||
                cartItem.getBusinessId() == null || cartItem.getBusinessId() == 0 ||
                cartItem.getFoodId() == null || cartItem.getFoodId() == 0
        ){
            return ResultBean.ERROR(10001,"CartItemService-update 请求参数为空");
        }
        int update = cartItemMapper.update(cartItem);
        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("CartItemService-update 修改购物车项失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {
        if (id == null){
            return ResultBean.ERROR(10001,"CartItemService-delete 请求参数为空");
        }
        int delete = cartItemMapper.delete(id);
        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("CartItemService-delete 删除购物车项失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {
        if (id == null){
            return ResultBean.ERROR(10001,"CartItemService-getById 请求参数为空");
        }
        CartItem cartItem = cartItemMapper.getById(id);
        return cartItem!=null?ResultBean.SUCCESS(cartItem):ResultBean.ERROR("CartItemService-getById 根据id查询购物车项失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<CartItem> cartItemList = cartItemMapper.getAll();
        return cartItemList!=null?ResultBean.SUCCESS(cartItemList):ResultBean.ERROR("CartItemService-getAll 获取全部购物车项信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {
        if (baseQuery == null){
            baseQuery = new CartItemQuery();
        }

        PageBean<CartItem> pageBean = new PageBean<>();
        //查询总记录
        int count = cartItemMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        /*if(count% baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }*/

        //查询集合
        List<CartItem> cartItemList = cartItemMapper.getByPage(baseQuery);
        pageBean.setData(cartItemList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids){
        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("CartItemService-batchDel 请求参数为空");
        }

        for (Integer id:ids){
            int delete = cartItemMapper.delete(id);
            if (delete < 1){
                throw new RuntimeException("CartItemService-batchDel 删除购物车项失败");
            }
        }

        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean getCartByBidAndUid(Integer userId, Integer businessId) {

        if (userId == null || userId == 0 || businessId == null || businessId == 0){
            return ResultBean.ERROR("请求参数为空值");
        }

        CartItemQuery cartItemQuery = new CartItemQuery();
        cartItemQuery.setUserId(userId);
        cartItemQuery.setBusinessId(businessId);

        List<CartItem> cartItemList = cartItemMapper.getByCondition(cartItemQuery);

        return ResultBean.SUCCESS(cartItemList);
    }
}
